//
//  GoodsTool.m
//  Store
//
//  Created by liujianlong on 2022/9/23.
//

#import "GoodsTool.h"
#import "FavoriteViewController.h"

@implementation GoodsTool
+ (NSArray<Goods*>*)find:(NSString *)type count:(NSInteger)count{
    NSString *name;
    if ([type isEqualToString:@"全部"]) {
        name = [NSString stringWithFormat:@"%@%d",type,arc4random()%5];
    }else if ([type isEqualToString:@"球鞋"]){
        name = [NSString stringWithFormat:@"%@%d",type,arc4random()%3];
    }else if ([type isEqualToString:@"篮球"]){
        name = [NSString stringWithFormat:@"%@%d",type,arc4random()%3];
    }else if ([type isEqualToString:@"足球"]){
        name = [NSString stringWithFormat:@"%@%d",type,arc4random()%2];
    }else if ([type isEqualToString:@"足球衫"]){
        name = [NSString stringWithFormat:@"%@%d",type,arc4random()%3];
    }else if ([type isEqualToString:@"随机"]){
        NSArray *brand = @[@"全部",@"球鞋",@"篮球",@"足球",@"足球衫"];
        NSMutableArray *array = [NSMutableArray array];
        return [self find:brand[arc4random()%5] count:count];
    }
    NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
    NSDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
    NSArray *array = [dict valueForKey:@"products"];
    
    NSArray *goodsarray = [Goods mj_objectArrayWithKeyValuesArray:array];
    if (goodsarray.count <= 0) {
        NSLog(@"");
    }
    
    ////随机获取
    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *nums = [NSMutableArray array];
    count = count <= 0 ? goodsarray.count : count;
    for (int i = 0; i < count; i++) {
        NSInteger n = arc4random()%goodsarray.count;
        while ([nums containsObject:@(n)]) {
             n = arc4random()%goodsarray.count;
        }
        [nums addObject:@(n)];
        [arr addObject:goodsarray[n]];
    }
    return arr;
}

+ (void)find:(GoodsOrderStastus)status complete:(nullable void(^) (NSArray <Goods *>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),bg_sqlKey(@"orderStatus"),bg_sqlValue(@(status))];
    NSInteger cout = [Goods bg_count:nil where:where];
    if (cout <= 0) {
        if (block) { return block(nil);}
    }
    [Goods bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block(array);
            }
        });
    }];
}
+ (void)delete:(Goods*)model complete:(nullable void(^) (void))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(model.account),bg_sqlKey(@"id"),bg_sqlValue(model.id),bg_sqlKey(@"orderStatus"),bg_sqlValue(@(model.orderStatus))];
    [Goods bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {block();}
        });
    }];
}
+ (void)findAll:(NSString *)name block:(nullable void(^) (NSMutableArray <Goods *>*result))block{
    NSArray *array1 = @[@"全部0",@"全部1",@"全部2",@"全部3",@"全部4"];
    NSArray *array2 = @[@"球鞋0",@"球鞋1",@"球鞋2"];
    NSArray *array3 = @[@"篮球0",@"篮球1",@"篮球2"];
    NSArray *array4 = @[@"足球0",@"足球1"];
    NSArray *array5 = @[@"足球衫0",@"足球衫1",@"足球衫2"];
    NSMutableArray *array = [NSMutableArray array];

    if ([@[LS(@"全部"),@"全部"] containsObject:name]) {
        [array addObjectsFromArray:array1];
    }else if ([@[LS(@"球鞋"),@"球鞋"] containsObject:name]){
        [array addObjectsFromArray:array2];
    }else if ([@[LS(@"篮球"),@"篮球"] containsObject:name]){
        [array addObjectsFromArray:array3];
    }else if ([@[LS(@"足球"),@"足球"] containsObject:name]){
        [array addObjectsFromArray:array4];
    }else if ([@[LS(@"足球衫"),@"足球衫"] containsObject:name]){
        [array addObjectsFromArray:array5];
    }else{
//        [array addObjectsFromArray:array1];
//        [array addObjectsFromArray:array2];
//        [array addObjectsFromArray:array3];
//        [array addObjectsFromArray:array4];
//        [array addObjectsFromArray:array5];
    }

    NSMutableArray *result = [NSMutableArray array];
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (NSString *name in array) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
            NSDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:file];
            NSArray *temp = [dict valueForKey:@"products"];
            
            NSArray *goodsarray = [Goods mj_objectArrayWithKeyValuesArray:temp];
            if (goodsarray.count > 0) {
                [result addObjectsFromArray:goodsarray];
            }
            dispatch_group_leave(group);
        });
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (block) {
            block(result);
        }
    });
}

@end
